<?php
    /**
     * P2C Admin platforms panel.
     *
     * @package WordPress
     * @subpackage Administration
     */

    if ( ! current_user_can( 'manage_options' ) )
        wp_die( __( 'You do not have sufficient permissions to manage options for this site.' ) );

    $action = p2c_current_action();

    $saved = -1;
	if ( 'save' == $action ) {
		$id = $_POST['pid'];
        
        $p2c_platform = new p2c_platform( $id );
        $p2c_platform->name = trim( $_POST['p2cpf-name'] );
        $p2c_platform->logo = trim( $_POST['p2cpf-logo'] );
        $p2c_platform->url = trim( $_POST['p2cpf-url'] );
        $p2c_platform->parse_key = trim( $_POST['p2cpf-key'] );
        $p2c_platform->parse_interval = trim( $_POST['p2cpf-interval'] );
        $p2c_platform->active =  $_POST['p2cpf-active'] ;
        $p2c_platform->country =  $_POST['p2cpf-country'] ;
        $p2c_platform->proxy =  $_POST['p2cpf-proxy'] ;

		$saved = $p2c_platform->save();
	}

    $list_table = new P2C_Platform_List_Table();
    $list_table->prepare_items();

	$_GET['pid'] = isset( $_GET['pid'] ) ? $_GET['pid'] : '';

	$p2cpf = p2c_platform( $_GET['pid'] );
    $pfid = $p2cpf->initial ? -1 : $p2cpf->id;

?>
<div class="wrap">
<?php screen_icon(); ?>

<h2><?php
    if($action == 'new') {
        echo esc_html( __( 'Add New Platform', 'p2c_admin' ) );
    } else {
        if(!$p2cpf->initial) {
            echo esc_html( __( 'Edit Platform', 'p2c_admin' ) );
        } else {
            echo esc_html( __( 'Platforms', 'p2c_admin' ) );
        }
	   echo ' <a href="?page=p2cpf&action=new" class="add-new-h2">' . esc_html( __( 'Add New', 'p2c_admin' ) ) . '</a>';
    }
?></h2>
<?php if($saved>-1) echo '<div id="message" class="updated"><p>Platform saved</p></div>'; 

if('new' == $action || 'edit' == $action) {
?>
<form method="post" action="<?php echo esc_url( add_query_arg( array( 'pid' => $pid ), menu_page_url( 'p2cpf', false ) ) ); ?>" id="p2cpf-form-element">
    <input type="hidden" id="pid" name="pid" value="<?php echo (int) $pfid; ?>" />
	<input type="hidden" id="hiddenaction" name="action" value="save" />

    <table class="form-table">
    <tr valign="top">
    <th scope="row"><label for="p2cpf-name">Platform name</label></th>
    <td><input type="text" id="p2cpf-name" name="p2cpf-name" class="regular-text" value="<?php echo esc_attr( $p2cpf->name ); ?>" /></td>
    </tr>
    <tr valign="top">
    <th scope="row"><label for="p2cpf-url">(Affiliate) link</label></th>
    <td><input type="text" id="p2cpf-url" name="p2cpf-url" class="regular-text" value="<?php echo esc_attr( $p2cpf->url ); ?>" style="width: 100%;" /></td>
    </tr>
    <tr valign="top">
    <th scope="row"><label for="p2cpf-logo">Logo</label></th>
    <td><input type="text" id="p2cpf-logo" name="p2cpf-logo" class="regular-text" value="<?php echo esc_attr( $p2cpf->logo ); ?>" style="width: 100%;" /></td>
    </tr>
    <tr valign="top">
    <th scope="row">Active</th>
    <td>
        <fieldset><legend class="screen-reader-text"><span>Active platform</span></legend>
        <label title='Yes'><input type='radio' name='p2cpf-active' value='1' <?php if($p2cpf->active) echo "checked='checked'"; ?> />Yes</label><br />
        <label title='No'><input type='radio' name='p2cpf-active' value='0' <?php if(!$p2cpf->active) echo "checked='checked'"; ?> />No</label><br />
        </fieldset>
    </td>
    </tr>
    <tr valign="top">
    <th scope="row"><label for="p2cpf-key">Parse Key</label></th>
    <td><input type="text" id="p2cpf-key" name="p2cpf-key" class="regular-text" value="<?php echo esc_attr( $p2cpf->parse_key ); ?>" style="width: 100%;" /></td>
    </tr>
    <tr valign="top">
    <th scope="row"><label for="p2cpf-interval">Parse Interval (hours)</label></th>
    <td><input type="text" id="p2cpf-interval" name="p2cpf-interval" class="regular-text" value="<?php echo esc_attr( $p2cpf->parse_interval ); ?>" style="width: 100%;" /></td>
    </tr>
    </tr>
    <tr valign="top">
    <th scope="row"><label for="p2cpf-country">Countries (comma separated)</label></th>
    <td><input type="text" id="p2cpf-country" name="p2cpf-country" class="regular-text" value="<?php echo esc_attr( $p2cpf->country ); ?>" style="width: 100%;" /></td>
    </tr>
    <tr valign="top">
    <th scope="row"><label for="p2cpf-proxy">Proxy</label></th>
    <td><input type="text" id="p2cpf-proxy" name="p2cpf-proxy" class="regular-text" value="<?php echo esc_attr( $p2cpf->proxy ); ?>" style="width: 100%;" /></td>
    </tr>
	</table>

    <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes"  /></p>
</form>
<?php
} elseif('parse' == $action) {
?>
    <table class="form-table">
    <tr valign="top">
    <th scope="row"><label for="p2cpf-name">Platform name</label></th>
    <td><input type="text" id="p2cpf-name" name="p2cpf-name" class="regular-text" disabled="disabled" value="<?php echo esc_attr( $p2cpf->name ); ?>" /></td>
    </tr>
	</table> 
<?php
    if ($p2cpf->parse_key)
    {
//        include_once( "includes/parser/" . $p2cpf->parse_key . ".php" );
//        parse_platform($batch, $p2cpf);
//        echo '<br/>Platform parsed: ' . $p2cpf->name;
        
        
        
        $batch = new P2C_Batch();
        $batch->save();
        
        if(!$batch->initial) {
        
            include_once( "includes/parser/" . $p2cpf->parse_key . ".php" );

            $batch->addPlatform($p2cpf);   
            $p2cpf->parse_date = $batch->start_date;
            $p2cpf->save();
            
            parse_platform($batch, $p2cpf);
            echo '<br/>Platform parsed: ' . $p2cpf->name;
            
            $batch->finish();
            echo '<br/>Batch voltooid';
        }
    }
} else {
?>
<form method="get" action="">
    <input type="hidden" name="page" value="<?php echo esc_attr( $_REQUEST['page'] ); ?>" />
	<?php $list_table->display(); ?>
</form>
<?php
}
?>

</div>